package com.gobang.config;

import com.gobang.api.MatchAPI;
import com.gobang.api.gameAPI;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
    @Resource
    MatchAPI matchAPI;
    @Autowired
    gameAPI gameAPI;
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        //添加 HTTP 会话拦截器，用于在 WebSocket 握手阶段传递 HTTP Session 数据。
        registry.addHandler(matchAPI,"/findMatch").addInterceptors(new HttpSessionHandshakeInterceptor());
        //.addInterceptors(new HttpSessionHandshakeInterceptor()
        // 是把HttpSession中的Attribute拿到WebsocketSession中。

        registry.addHandler(gameAPI,"/game").addInterceptors(new HttpSessionHandshakeInterceptor());
    }
}
